#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
const int N = 1e6 + 5;

vector<int> pre;
vector<int> suf;

class Solution {
    int len = 0;  // 用于记录中序遍历的当前位置
   public:
    vector<int> solve(int n, vector<int>& pre, vector<int>& suf) {
        vector<int> v(n);
        map<int, int> mp;  // 用来存储根的位置，方便在后续遍历中查找
        for (int i = 0; i < n; i++) {
            mp[suf[i]] = i;
        }
        deal(0, n - 1, pre, 0, n - 1, suf, mp, v);
        return v;
    }

    void deal(int pl, int pr, vector<int>& pre, int sl, int sr,
              vector<int>& suf, map<int, int>& mp, vector<int>& v) {
        if (pl > pr) return;

        if (pl == pr) {
            v[len++] = pre[pl];
            return;
        }

        int root = pre[pl];
        int pos = mp[pre[pl + 1]];  // 查找左子树根节点在右子树的哪个位置

        int lsize = pos - sl + 1;

        deal(pl + 1, pl + 1 + lsize - 1, pre, sl, pos, suf, mp, v);  // 递归处理左子树
        v[len++] = root;
        deal(pl + lsize + 1, pr, pre, pos + 1, sr - 1, suf, mp, v);  // 递归处理右子树
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n;
    cin >> n;
    pre.resize(n);
    suf.resize(n);

    for (int i = 0; i < n; ++i) {
        cin >> pre[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> suf[i];
    }

    Solution sol;
    vector<int> ans = sol.solve(n, pre, suf);

    for (int i = 0; i < ans.size(); ++i) {
        if (i > 0) cout << " ";
        cout << ans[i];
    }
    cout << endl;

    return 0;
}
